﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
using PlayPrima;

namespace Playkanaler
{
    public class PrimaCat : Setting
    {


        public const uint WM_LBUTTONDOWN = 0x0201;
        public const uint WM_LBUTTONUP = 0x0202;

        [DllImport("user32.dll")]
        public static extern IntPtr GetWindow(IntPtr hWnd, uint Msg);

        [DllImport("gdi32.dll")]
        public static extern Int32 GetPixel(Int32 hdc, Int32 x, Int32 y);
        [DllImport("user32.dll")]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
        [DllImport("user32.dll")]
        static extern int GetWindowText(int hWnd, StringBuilder text, int count);




        SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
        object Empty = 0;
        object URL = "http://playprima.se/";
        const int bwidth = 1214;
        const int bheight = 712;



        //för att avgöra hur många pixlar som ska dras i scrollbaren
        int scrolllen(int count, int index)
        {
            return (int)(((368 / (count * 48 / 402)) / 8.4) * index);
        }

        //välj ett av avsnitt
        public void SetEpiIndex(int index, int count)
        {

            if (index != -1)
            {
                int sel = index;
                if (index > 8) // om index är större än 8 måste man scrolla
                {
                    int xm = bwidth / 2 + 372;
                    int ym = 166 + scrolllen(count, index - 8);

                    //System.Windows.Forms.MessageBox.Show("y1=166 y2=" + ym);

                    dragWnd(new IntPtr(IE.HWND), xm, 166, xm, ym);
                    sel = 8;
                    //System.Threading.Thread.Sleep(100);
                    //dragBrowser(new IntPtr(IE.HWND), xm, 166, xm, ym);
                }

                int x = bwidth / 2 + 200;
                int y = 145 + sel * 49;

                //System.Threading.Thread.Sleep(100);
                clickWnd(new IntPtr(IE.HWND), x, y);
                //MessageBox.Show("x=" + x.ToString() + " y=" + y.ToString());
            }
        }
        IntPtr getBrowserWnd(IntPtr wnd)
        {
            IntPtr window = new IntPtr(0);
            window = FindWindowEx(wnd, new IntPtr(0), "Frame Tab", null);
            window = FindWindowEx(window, new IntPtr(0), "TabWindowClass", null);
            window = FindWindowEx(window, new IntPtr(0), "Shell DocObject View", null);
            window = GetWindow(window, 5);

            return window;

        }

        void clickWnd(IntPtr wnd, int x, int y)
        {

            int lParam = (y << 16) + x;
            IntPtr hwnd = getBrowserWnd(wnd);


            SendMessage(hwnd, 0x0200, 0x0, lParam);

            SendMessage(hwnd, WM_LBUTTONDOWN, 0x1, lParam);
            SendMessage(hwnd, 0x0200, 0x1, lParam);
            SendMessage(hwnd, WM_LBUTTONUP, 0x0, lParam);

        }
        void dragWnd(IntPtr wnd, int x, int y, int tx, int ty)
        {
            int lParam = (y << 16) + x;
            IntPtr hwnd = getBrowserWnd(wnd);

            SendMessage(hwnd, 0x0200, 0x0, lParam);
            SendMessage(hwnd, WM_LBUTTONDOWN, 0x1, lParam);

            lParam = (ty << 16) + tx;

            SendMessage(hwnd, 0x0200, 0x1, lParam);
            SendMessage(hwnd, WM_LBUTTONUP, 0x0, lParam);

        }




        bool loaded()
        {
            Graphics g = Graphics.FromHwnd(getBrowserWnd(new IntPtr(IE.HWND)));
            Int32 hdc = g.GetHdc().ToInt32();

            Int32 rgb = GetPixel(hdc, 700, 150);

            if (rgb != 2895144 && rgb != 16777215)
            {
                return true;
            }
            return false;
        }






        Microsoft.MediaCenter.UI.Timer timer = new Microsoft.MediaCenter.UI.Timer();
        public override void Load()
        {
            timer.Tick += new EventHandler(t_Tick);
        }
        bool done = false;
        int plinx = -1;
        void t_Tick(object sender, EventArgs e)
        {

            if (!done)
            {
                done = loaded();
                System.Threading.Thread.Sleep(100);
            }
            else if (plinx > -1)
            {
                Play();
                plinx = -2;
            }
            if (plinx == -2)
            {
                if (cliploaded())
                {
                    clickWnd(new IntPtr(IE.HWND), 700, 450);

                    plinx = -1;

                }

            }
        }


        //avgör om den är redo för att trycka på helskärm
        bool cliploaded()
        {

            Graphics g = Graphics.FromHwnd(getBrowserWnd(new IntPtr(IE.HWND)));
            Int32 hdc = g.GetHdc().ToInt32();

            Int32 rgb = GetPixel(hdc, 400, 450);

            if (rgb == 15329769)
            {
                return true;
            }
            return false;
        }



        //när användaren väljer ett klipp...
        public override void ClickItem(Item item)
        {

            IE.Visible = true;
            plinx = item.Index;

        }
        public void Play()
        {

            SetEpiIndex(plinx, ((Group)SelectedTab[0]).Count);

        }




        public PrimaCat()
        {
            ListView listview = new ListView();

            View = listview;




            SelectedTabIndex = -1;

            Heading = "SvtPlay";
        }
        public PrimaCat(SPCategory cat, SHDocVw.InternetExplorer ie)
        {
            ListView listview = new ListView();

            View = listview;

            IE = ie;


            URL = "about:blank";
            IE.Navigate2(ref URL, ref Empty, ref Empty, ref Empty, ref Empty);
            System.Threading.Thread.Sleep(100);
            URL = "http://playprima.se/#|" + cat.id + "|";
            IE.Navigate2(ref URL, ref Empty, ref Empty, ref Empty, ref Empty);
            timer.Interval = 1000;
            timer.AutoRepeat = true;
            timer.Start();



            Tab tab = new Tab("Prima", 0);
            Group g = new Group();
            foreach (SPClip clip in cat.Clip)
            {
                Item it = new Item();
                it.Index = g.Count;
                it.Title = clip.Title;
                it.Description = clip.Desc;
                it.Attributes["id"] = clip.id;
                g.Add(it);

            }
            tab.Add(g);
            Tabs.Add(tab);



            SelectedTabIndex = 0;

            Heading = cat.Title;
        }
    }
}
